﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using sqlDALgen;
using System.Data;
using System.Data.SqlClient;

namespace codewriter
{
    public class DbUtil
    {
        public static TemplateData LoadTableSchema(string name, string schema, string connectionString)
        {
            Console.WriteLine(String.Format(""));
            Console.WriteLine(String.Format("Fetching data for table: '{0}', schema: '{1}'", name, schema));

            DataSet retVal = new DataSet();

            using (var conn = new SqlConnection(connectionString))
            {
                conn.Open();

                var command = new SqlCommand(String.Format("sp_help '[{0}].[{1}]'", schema, name), conn);
                var adapter = new SqlDataAdapter(command);
                adapter.Fill(retVal);

                conn.Close();
            }

            return TemplateData.FromDataset(retVal);
        }
    }
}
